package com.mangatta.cloud.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

public abstract class DbHelper extends SQLiteOpenHelper {
    private static final String TAG = DbHelper.class.getSimpleName();
    public static final String DB_NAME = "mangatta.db";
    public static final int DB_VERSION = 1;

    //INSTANCE_DETAILS TABLE.
    public static final class InstanceDetailTable {
        public static final String TABLE_INSTANCE_DETAILS = "instance_details";
        public static final String C_INSTANCE_ID = "instance_id";
        public static final String C_CREATED_AT = "created_at";
        public static final String C_NAME = "instance_name";
        public static final String C_STATE = "state";

        private InstanceDetailTable() {
        }

        private static String createTableQuery() {
            return "create table " + TABLE_INSTANCE_DETAILS + "("
                    + C_INSTANCE_ID + " text primary key, "
                    + C_CREATED_AT + " int, "
                    + C_STATE + " int,"
                    + C_NAME + " text"
                    + ")";
        }
    }

    //ALARM_CONFIG TABLE.
    public static final class AlarmConfigurationTable {
        public static final String TABLE_ALARM_CONFIGURATIONS = "alarm_configurations";
        public static final String C_ID = BaseColumns._ID;
        public static final String C_INSTANCE = "instance";
        public static final String C_METRIC = "metric";
        public static final String C_THRESHOLD = "threshold";
        public static final String C_STATISTIC = "statistic";
        public static final String C_UNIT = "unit";
        public static final String C_CREATED_AT = "created_at";

        private AlarmConfigurationTable() {
        }

        private static String createTableQuery() {
            return "create table " + TABLE_ALARM_CONFIGURATIONS + "("
                    + C_ID + " int primary key, "
                    + C_CREATED_AT + " int, "
                    + C_INSTANCE + " text, "
                    + C_METRIC + " text, "
                    + C_THRESHOLD + " double, "
                    + C_STATISTIC + " text, "
                    + C_UNIT + " text"
                    + ")";
        }
    }

    // INSTANCE_STATUSES TABLE
    public static final class InstanceStatusTable {
        public static final String TABLE_INSTANCE_STATUSES = "instance_statuses";
        public static final String C_INSTANCE = "instance";
        public static final String C_METRIC = "metric";
        public static final String C_VALUE = "value";
        public static final String C_STATISTIC = "statistic";
        public static final String C_UNIT = "unit";
        public static final String C_SAMPLED_AT = "sampled_at";
        public static final String C_SAMPLE_COUNT = "sample_count";

        private InstanceStatusTable() {
        }

        private static String createTableQuery() {
            return "create table " + TABLE_INSTANCE_STATUSES + "("
                    + C_INSTANCE + " text, "
                    + C_METRIC + " text, "
                    + C_VALUE + " double, "
                    + C_STATISTIC + " text, "
                    + C_UNIT + " text, "
                    + C_SAMPLED_AT + " int, "
                    + C_SAMPLE_COUNT + " int"
                    + ")";
        }
    }

    public DbHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(InstanceDetailTable.createTableQuery());
        Log.d(TAG, "Created table " + InstanceDetailTable.TABLE_INSTANCE_DETAILS);

        db.execSQL(AlarmConfigurationTable.createTableQuery());
        Log.d(TAG, "Created table " + AlarmConfigurationTable.TABLE_ALARM_CONFIGURATIONS);

        db.execSQL(InstanceStatusTable.createTableQuery());
        Log.d(TAG, "Created table " + InstanceStatusTable.TABLE_INSTANCE_STATUSES);
    }


    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        Log.d(TAG, "onUpgraded");
    }

}
